import os
import json
class Subject:
    def __init__(self):
        self.observers = []

    def register_observer(self, observer):
        self.observers.append(observer)

    def unregister_observer(self, observer):
        self.observers.remove(observer)

    def notify_observers(self, data):
        for observer in self.observers:
            observer.update(data)

class Observer:
    def __init__(self,redis_client,save_dir):
        self.client = redis_client
        self.save_dir = save_dir

    def update(self, data):
        raise NotImplementedError("Subclasses should implement this method")

class StatusObserver(Observer):
    def update(self, data):
        for key in data:
            if 'status' in key:
                if not os.path.exists(self.save_dir):
                    os.mkdir(self.save_dir)
                file_path = os.path.join(self.save_dir,"status.json")
                with open(file_path, 'w', encoding='utf-8') as file:
                    json_str = json.dumps(data[key],ensure_ascii=False)
                    file.write(json_str)
                break

class ProgressObserver(Observer):
    def update(self, data):
        for key in data:
            if 'calcprog' in key:
                progress = data.get(key)
                self.client.set(key, progress)
                break